import request from '@/utils/request';
import type { RoleResponseData, RoleData, MenuResponseData } from './type';

enum API {
  //获取全部角色
  ALLROLE_URL = '/admin/acl/role/',
  //新增岗位的接口地址
  ADDROLE_URL = '/admin/acl/role/save',
  //更新已有职位
  UPDATEROLE_URL = '/admin/acl/role/update',
  //获取全部权限和当前角色权限数据
  ALLPERMISSION_URL = '/admin/acl/permission/toAssign/',
  //给相应的职位分配权限
  SETPERMISSION_URL = '/admin/acl/permission/doAssign',
  //删除已有职位
  REMOVEROLE_URL = '/admin/acl/role/remove/',
}

/**
 *
 *  获取全部角色
 * @param pageNo 页数
 * @param limit 单页数据量
 * @param roleName 角色名字
 */
export const reqAllRole = (pageNo: number, limit: number, roleName: string) => {
  return request<any, RoleResponseData>({
    url: `${API.ALLROLE_URL}/${pageNo}/${limit}/?roleName=${roleName}`,
    method: 'get',
  });
};

/**
 *
 *  添加与更新职位
 * @param data RoleData
 */
export const reqAddOrUpdateRole = (data: RoleData) => {
  if (data.id) {
    return request<any, any>({
      url: API.UPDATEROLE_URL,
      method: 'put',
      data,
    });
  } else {
    return request<any, any>({
      url: API.ADDROLE_URL,
      method: 'post',
      data,
    });
  }
};

/**
 *
 *  获取全部权限和当前角色权限数据
 * @param roleId 角色ID
 */
export const reqAllPermission = (roleId: number) => {
  return request<any, MenuResponseData>({
    url: `${API.ALLPERMISSION_URL}${roleId}`,
    method: 'get',
  });
};

/**
 *
 *  给相应的职位下发权限
 * @param roleId 角色ID
 * @param permissionId 权限ID列表
 */
export const reqSetPermission = (roleId: number, permissionId: number[]) => {
  return request<any, any>({
    url: `${API.SETPERMISSION_URL}/?roleId=${roleId}&permissionId=${permissionId}`,
    method: 'post',
  });
};

/**
 *
 *  删除已有职位
 * @param roleId 角色ID
 */
export const reqRemoveRole = (roleId: number) => {
  return request<any, any>({
    url: `${API.REMOVEROLE_URL}${roleId}`,
    method: 'delete',
  });
};
